<template>
  <div class="main h-[calc(100%-29px)]">
    <div class="w-full">
      <div v-if="queryShow" class="mb-[10px] w-full search-form">
        <el-form :inline="true" :model="queryForm.data" label-width="0px">
          <el-form-item>
            <el-input
              v-model="queryForm.data.blurry"
              clearable
              placeholder="输入储能名称搜索"
            />
          </el-form-item>
          <el-form-item class="ml-[10px]">
            <el-button type="primary" :icon="Search" @click="search"
              >搜索</el-button
            >
            <el-button :icon="RefreshLeft" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="w-full">
        <div class="w-full flex justify-between">
          <div class="buttons flex align-center">
            <el-button
              v-if="handleStatus !== 'look'"
              type="primary"
              :icon="Plus"
              @click="addMenu"
              >新增</el-button
            >
            <el-button
              v-if="handleStatus !== 'look'"
              type="success"
              :icon="Edit"
              :disabled="!(multipleSelection.length === 1)"
              @click="editSelect"
              >修改</el-button
            >
            <el-button
              v-if="handleStatus !== 'look'"
              type="danger"
              :icon="Delete"
              :disabled="!multipleSelection.length"
              @click="delSelect"
              >删除</el-button
            >
            <!--            <el-button type="warning" :icon="Download" @click="exportData"-->
            <!--              >导出</el-button-->
            <!--            >-->
          </div>
          <div class="settings">
            <el-button-group class="ml-4">
              <el-button
                type="primary"
                :icon="Search"
                @click="queryShow = !queryShow"
              />
              <el-button type="primary" :icon="RefreshLeft" @click="getList" />
              <el-popover placement="bottom" :width="200" trigger="click">
                <template #reference>
                  <el-button type="primary" :icon="Grid" />
                </template>
                <el-checkbox
                  v-model="checkAll"
                  :indeterminate="isIndeterminate"
                  @change="handleCheckAllChange"
                  >全选</el-checkbox
                >
                <div class="group-box w-full max-h-[240px] overflow-y-auto">
                  <el-checkbox-group
                    v-model="checkedCities"
                    class="columns-show-group"
                    @change="handleCheckedCitiesChange"
                  >
                    <el-checkbox
                      v-for="item in defaultColums.data"
                      :key="item.prop"
                      :label="item.prop"
                      >{{ item.label }}</el-checkbox
                    >
                  </el-checkbox-group>
                </div>
              </el-popover>
            </el-button-group>
          </div>
        </div>
        <div class="flex">
          <div class="role w-full">
            <div v-loading="loading" class="w-full mt-[10px]">
              <el-table
                ref="multipleTableRef"
                :data="tableData.data"
                style="width: 100%"
                row-key="id"
                @selection-change="handleSelectionChange"
              >
                <el-table-column type="selection" width="55" />
                <el-table-column
                  v-for="item in colums.data"
                  :key="item.prop"
                  :prop="item.prop"
                  :label="item.label"
                  :width="item.width"
                  show-overflow-tooltip
                >
                  <template
                    v-if="['status'].indexOf(item.prop) > -1"
                    #default="scope"
                  >
                    <span>{{
                      dictFormat(
                        item.prop,
                        scope.row[item.prop],
                        dictList.data[item.prop]
                      )
                    }}</span>
                  </template>
                </el-table-column>
                <el-table-column fixed="right" label="操作" width="260">
                  <template #default="scope">
                    <el-tooltip
                      class="box-item"
                      effect="dark"
                      content="功率数据"
                      placement="top"
                    >
                      <el-button
                        type="primary"
                        size="small"
                        :icon="DataLine"
                        @click="pTrends(scope.row)"
                      />
                    </el-tooltip>
                    <el-tooltip
                      class="box-item"
                      effect="dark"
                      content="查看"
                      placement="top"
                    >
                      <el-button
                        type="primary"
                        size="small"
                        :icon="Document"
                        @click="lookRow(scope.row)"
                      />
                    </el-tooltip>
                    <el-tooltip
                      class="box-item"
                      effect="dark"
                      content="修改"
                      placement="top"
                    >
                      <el-button
                        v-if="handleStatus !== 'look'"
                        type="primary"
                        size="small"
                        :icon="Edit"
                        @click="editRow(scope.row)"
                      />
                    </el-tooltip>
                    <el-popconfirm
                      v-if="handleStatus !== 'look'"
                      width="200px"
                      title="确定删除本条数据吗？"
                      @confirm="delConfirm(scope.row)"
                    >
                      <template #reference>
                        <el-button type="danger" size="small" :icon="Delete" />
                      </template>
                    </el-popconfirm>
                  </template>
                </el-table-column>
              </el-table>
            </div>
            <div class="page w-full mt-[10px]">
              <el-pagination
                v-model:current-page="pageObj.data.currentPage"
                v-model:page-size="pageObj.data.pageSize"
                :page-sizes="[10, 20, 30, 40, 50, 100]"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pageObj.data.total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </div>
          </div>
        </div>
      </div>
    </div>
    <el-drawer
      v-model="addDialog"
      :title="title"
      :close-on-click-modal="false"
      :before-close="beforeClose"
      size="80%"
    >
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="cancel">取消</el-button>
          <el-button
            v-if="handleStatus !== 'look' && status !== 'look'"
            type="primary"
            @click="submit"
          >
            确认
          </el-button>
        </span>
      </template>
      <el-form
        ref="ruleForm"
        label-width="180px"
        :model="form.data"
        :rules="form.rules"
        :disabled="handleStatus === 'look' || status === 'look'"
      >
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="储能名称" prop="name">
              <el-input v-model="form.data.name" placeholder="储能名称" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="储能系统序号">
              <el-input-number
                v-model="form.data.storSeqNo"
                style="width: 100%"
                placeholder="储能系统序号"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="储能厂家" prop="storMaker">
              <el-input v-model="form.data.storMaker" placeholder="储能厂家" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="储能EMS厂家" prop="storEmsMaker">
              <el-input
                v-model="form.data.storEmsMaker"
                placeholder="储能EMS厂家"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="储能EMS系统IP地址">
              <el-input
                v-model="form.data.storEmsIpadd"
                placeholder="储能EMS系统IP地址"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="经度">
              <el-input-number
                v-model="form.data.longitude"
                style="width: 100%"
                placeholder="经度"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="纬度">
              <el-input-number
                v-model="form.data.latitude"
                style="width: 100%"
                placeholder="纬度"
              />
            </el-form-item>
          </el-col>
          <!--          <el-col :span="8">-->
          <!--            <el-form-item label="经纬度(虚拟字段)">-->
          <!--              <el-input-->
          <!--                v-model="form.data.longitudeAndLatitude"-->
          <!--                placeholder="经纬度(虚拟字段)"-->
          <!--              />-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <el-col :span="8">
            <el-form-item label="最大充电功率(kW)">
              <el-input-number
                v-model="form.data.maxLoad"
                style="width: 100%"
                placeholder="最大充电功率(kW)"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="最大放电功率(kW)">
              <el-input-number
                v-model="form.data.maxOutput"
                style="width: 100%"
                placeholder="最大放电功率(kW)"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="最大调节步长(kW)">
              <el-input-number
                v-model="form.data.maxAdjustStep"
                style="width: 100%"
                placeholder="最大调节步长(kW)"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="正常调节速率(kW/min)">
              <el-input-number
                v-model="form.data.normalAdjustSpeed"
                style="width: 100%"
                placeholder="正常调节速率(kW/min)"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="最大上升速率(kW/min)">
              <el-input-number
                v-model="form.data.maxUpSpeed"
                style="width: 100%"
                placeholder="最大上升速率(kW/min)"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="最大下降速率(kW/min)">
              <el-input-number
                v-model="form.data.maxDownSpeed"
                style="width: 100%"
                placeholder="最大下降速率(kW/min)"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="荷电容量上限">
              <el-input-number
                v-model="form.data.socUpperLimit"
                style="width: 100%"
                placeholder="荷电容量上限"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="荷电容量下限">
              <el-input-number
                v-model="form.data.socLowerLimit"
                style="width: 100%"
                placeholder="荷电容量下限"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="单位充电成本(元/kWh)">
              <el-input-number
                v-model="form.data.unitChargeCost"
                style="width: 100%"
                placeholder="单位充电成本(元/kWh)"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="单位放电成本(元/kWh)">
              <el-input-number
                v-model="form.data.unitDischargeCost"
                style="width: 100%"
                placeholder="单位放电成本(元/kWh)"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="充电效率">
              <el-input-number
                v-model="form.data.chargeRatio"
                style="width: 100%"
                placeholder="充电效率"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="放电效率">
              <el-input-number
                v-model="form.data.dischargeRatio"
                style="width: 100%"
                placeholder="放电效率"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="自放电效率">
              <el-input-number
                v-model="form.data.selfDischargeRatio"
                style="width: 100%"
                placeholder="自放电效率"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="自用电效率">
              <el-input-number
                v-model="form.data.selfUseRatio"
                style="width: 100%"
                placeholder="自用电效率"
              />
            </el-form-item> </el-col
          ><el-col :span="8">
            <el-form-item label="最小充电时长(h)">
              <el-input-number
                v-model="form.data.minChargeTime"
                style="width: 100%"
                placeholder="最小充电时长(h)"
              />
            </el-form-item> </el-col
          ><el-col :span="8">
            <el-form-item label="最小放电时长(h)">
              <el-input-number
                v-model="form.data.minDischargeTime"
                style="width: 100%"
                placeholder="最小放电时长(h)"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="资源状态">
              <!--              <el-input v-model="form.data.maxAdjustStep" placeholder="资源状态" />-->
              <el-select
                v-model="form.data.status"
                clearable
                placeholder="请选择"
              >
                <el-option
                  v-for="item in dictList.data.status"
                  :key="item.id"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="储能上报状态">
              <el-input-number
                v-model="form.data.storStatus"
                style="width: 100%"
                placeholder="储能上报状态"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="首次上线时间" prop="initOnlineTime">
              <el-date-picker
                v-model="form.data.initOnlineTime"
                value-format="YYYY-MM-DD HH:mm:ss"
                date-format="YYYY-MM-DD HH:mm:ss"
                style="width: 100%"
                type="datetime"
                placeholder="请选择"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="上线时间" prop="onlineTime">
              <el-date-picker
                v-model="form.data.onlineTime"
                value-format="YYYY-MM-DD HH:mm:ss"
                date-format="YYYY-MM-DD HH:mm:ss"
                style="width: 100%"
                type="datetime"
                placeholder="请选择"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="下线时间" prop="offlineTime">
              <el-date-picker
                v-model="form.data.offlineTime"
                value-format="YYYY-MM-DD HH:mm:ss"
                date-format="YYYY-MM-DD HH:mm:ss"
                style="width: 100%"
                type="datetime"
                placeholder="请选择"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="有功遥测点号">
              <el-input v-model="form.data.pKey" placeholder="有功遥测点号" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="无功遥测点号">
              <el-input v-model="form.data.qKey" placeholder="无功遥测点号" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="SOC遥测点号">
              <el-input v-model="form.data.socKey" placeholder="SOC遥测点号" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="储能状态点号">
              <el-input
                v-model="form.data.storStatusKey"
                placeholder="储能状态点号"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="总利润(元)">
              <el-input-number
                v-model="form.data.totalRevenue"
                style="width: 100%"
                placeholder="总利润(元)"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="分成利润(元)">
              <el-input-number
                v-model="form.data.shareRevenue"
                style="width: 100%"
                placeholder="分成利润(元)"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="总有功(kW)">
              <el-input-number
                v-model="form.data.totalP"
                style="width: 100%"
                placeholder="总有功(kW)"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="总无功(kVar)">
              <el-input-number
                v-model="form.data.totalQ"
                style="width: 100%"
                placeholder="总无功(kVar)"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系人">
              <el-input v-model="form.data.contactName" placeholder="联系人" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系电话">
              <el-input v-model="form.data.contactTel" placeholder="联系电话" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-drawer>
  </div>
</template>

<script setup lang="ts">
import {
  DataLine,
  Delete,
  Document,
  Download,
  Edit,
  Grid,
  Plus,
  RefreshLeft,
  Search
} from "@element-plus/icons-vue";
import { ElForm, ElMessageBox, ElNotification, ElTable } from "element-plus";
import { nextTick, onMounted, reactive, ref } from "vue";
import { getDictData } from "@/api/system";
import { downloadFile } from "@/utils";
import { useRouter } from "vue-router";
import { cloneDeep } from "@pureadmin/utils";
import {
  addVppStorageList,
  delVppStorageList,
  editVppStorageList,
  exportVppStorageList,
  getVppStorageList
} from "@/api/vppStorage";

const props = defineProps({
  activeVppUserId: {
    type: String
  },
  handleStatus: {
    type: String,
    default: "add"
  }
});
const router = useRouter();
const queryShow = ref(true);
const queryForm = reactive({
  data: {
    blurry: undefined,
    enabled: undefined,
    createTime: []
  }
});
const pTrends = row => {
  router.push({
    path: "/orderPeopleUse/storagePredictingTrends",
    query: {
      storeId: row.id,
      id: props.activeVppUserId
    }
  });
};
const resetQuery = () => {
  queryForm.data = {
    blurry: undefined,
    enabled: undefined,
    createTime: []
  };
};
const search = async () => {
  pageObj.data.currentPage = 1;
  let { content, totalElements } = await getData();
  pageObj.data.total = totalElements;
  tableData.data = content;
};
const getList = async () => {
  let { content, totalElements } = await getData();
  pageObj.data.total = totalElements;
  tableData.data = content;
};
const exportData = async () => {
  let res = await exportVppStorageList({
    current: pageObj.data.currentPage - 1,
    size: pageObj.data.pageSize,
    sort: "id,desc",
    name: queryForm.data.blurry ? queryForm.data.blurry : undefined,
    vppUserId: props.activeVppUserId ? props.activeVppUserId : undefined
  });
  downloadFile(res, "储能数据", "xlsx");
};
const handleSizeChange = () => {
  search();
};
const handleCurrentChange = async () => {
  getList();
};
const getData = () => {
  loading.value = true;
  return new Promise<any>(async (resolve, reject) => {
    let data = await getVppStorageList({
      current: pageObj.data.currentPage,
      size: pageObj.data.pageSize,
      sort: "id,desc",
      name: queryForm.data.blurry ? queryForm.data.blurry : undefined,
      vppUserId: props.activeVppUserId ? props.activeVppUserId : undefined
    });
    loading.value = false;
    resolve(data);
  });
};
// const currentLevel = ref(3);

const dictList = reactive({
  data: {
    status: []
  }
});
const init = async () => {
  const vppTerminalAssetsStatus = await getDictData({
    dictName: "vpp_terminal_assets_status"
  });
  dictList.data.status = vppTerminalAssetsStatus.content.map(item => ({
    ...item,
    value: parseInt(item.value)
  }));
};
const dictFormat = (key, value, data) => {
  if (value) {
    let item = data.find(i => parseInt(i.value) === parseInt(value));
    if (item) {
      return item.label;
    } else {
      return "";
    }
  } else {
    return "";
  }
};
init();
const tableData = reactive({
  data: []
});
const colums = reactive({
  data: [
    {
      label: "储能名称",
      prop: "name",
      width: "100px"
    },
    // {
    //   label: "虚拟电厂用户ID",
    //   prop: "vppUserId",
    //   width: "160px"
    // },
    {
      label: "储能系统序号",
      prop: "storSeqNo",
      width: "140px"
    },
    {
      label: "储能厂家",
      prop: "storMaker",
      width: "140px"
    },
    {
      label: "储能EMS厂家",
      prop: "storEmsMaker",
      width: "140px"
    },
    {
      label: "储能EMS系统IP地址",
      prop: "storEmsIpadd",
      width: "180px"
    },
    {
      label: "经度",
      prop: "longitude",
      width: "100px"
    },
    {
      label: "纬度",
      prop: "latitude",
      width: "100px"
    },
    // {
    //   label: "经纬度(虚拟字段)",
    //   prop: "longitudeAndLatitude",
    //   width: "170px"
    // },
    {
      label: "最大充电功率(kW)",
      prop: "maxLoad",
      width: "180px"
    },
    {
      label: "最大放电功率(kW)",
      prop: "maxOutput",
      width: "180px"
    },
    {
      label: "最大调节步长(kW)",
      prop: "maxAdjustStep",
      width: "180px"
    },
    {
      label: "正常调节速率(kW/min)",
      prop: "normalAdjustSpeed",
      width: "180px"
    },
    {
      label: "最大上升速率(kW/min)",
      prop: "maxUpSpeed",
      width: "180px"
    },
    {
      label: "最大下降速率(kW/min)",
      prop: "maxDownSpeed",
      width: "180px"
    },
    {
      label: "荷电容量上限",
      prop: "socUpperLimit",
      width: "150px"
    },
    {
      label: "荷电容量下限",
      prop: "socLowerLimit",
      width: "150px"
    },
    {
      label: "单位充电成本(元/kWh)",
      prop: "unitChargeCost",
      width: "180px"
    },
    {
      label: "单位放电成本(元/kWh)",
      prop: "unitDischargeCost",
      width: "180px"
    },
    {
      label: "充电效率",
      prop: "chargeRatio",
      width: "120px"
    },
    {
      label: "放电效率",
      prop: "dischargeRatio",
      width: "120px"
    },
    {
      label: "自放电效率",
      prop: "selfDischargeRatio",
      width: "130px"
    },
    {
      label: "自用电效率",
      prop: "selfUseRatio",
      width: "130px"
    },
    {
      label: "最小充电时长(h)",
      prop: "minChargeTime",
      width: "150px"
    },
    {
      label: "最小放电时长(h)",
      prop: "minDischargeTime",
      width: "150px"
    },
    {
      label: "资源状态",
      prop: "status",
      width: "120px"
    },
    {
      label: "储能上报状态",
      prop: "storStatus",
      width: "140px"
    },
    {
      label: "首次上线时间",
      prop: "initOnlineTime",
      width: "140px"
    },
    {
      label: "上线时间",
      prop: "onlineTime",
      width: "120px"
    },
    {
      label: "下线时间",
      prop: "offlineTime",
      width: "120px"
    },
    {
      label: "有功遥测点号",
      prop: "pKey",
      width: "140px"
    },
    {
      label: "无功遥测点号",
      prop: "qKey",
      width: "140px"
    },
    {
      label: "SOC遥测点号",
      prop: "socKey",
      width: "140px"
    },
    {
      label: "储能状态点号",
      prop: "storStatusKey",
      width: "140px"
    },
    {
      label: "总利润(元)",
      prop: "totalRevenue",
      width: "100px"
    },
    {
      label: "分成利润(元)",
      prop: "shareRevenue",
      width: "120px"
    },
    {
      label: "总有功(kW)",
      prop: "totalP",
      width: "120px"
    },
    {
      label: "总无功(kVar)",
      prop: "totalQ",
      width: "120px"
    },
    {
      label: "联系人",
      prop: "contactName",
      width: "120px"
    },
    {
      label: "联系电话",
      prop: "contactTel",
      width: "120px"
    }
  ]
});
const defaultColums = reactive({
  data: [
    {
      label: "储能名称",
      prop: "name",
      width: "100px"
    },
    // {
    //   label: "虚拟电厂用户ID",
    //   prop: "vppUserId",
    //   width: "160px"
    // },
    {
      label: "储能系统序号",
      prop: "storSeqNo",
      width: "140px"
    },
    {
      label: "储能厂家",
      prop: "storMaker",
      width: "140px"
    },
    {
      label: "储能EMS厂家",
      prop: "storEmsMaker",
      width: "140px"
    },
    {
      label: "储能EMS系统IP地址",
      prop: "storEmsIpadd",
      width: "180px"
    },
    {
      label: "经度",
      prop: "longitude",
      width: "100px"
    },
    {
      label: "纬度",
      prop: "latitude",
      width: "100px"
    },
    // {
    //   label: "经纬度(虚拟字段)",
    //   prop: "longitudeAndLatitude",
    //   width: "170px"
    // },
    {
      label: "最大充电功率(kW)",
      prop: "maxLoad",
      width: "180px"
    },
    {
      label: "最大放电功率(kW)",
      prop: "maxOutput",
      width: "180px"
    },
    {
      label: "最大调节步长(kW)",
      prop: "maxAdjustStep",
      width: "180px"
    },
    {
      label: "正常调节速率(kW/min)",
      prop: "normalAdjustSpeed",
      width: "180px"
    },
    {
      label: "最大上升速率(kW/min)",
      prop: "maxUpSpeed",
      width: "180px"
    },
    {
      label: "最大下降速率(kW/min)",
      prop: "maxDownSpeed",
      width: "180px"
    },
    {
      label: "荷电容量上限",
      prop: "socUpperLimit",
      width: "150px"
    },
    {
      label: "荷电容量下限",
      prop: "socLowerLimit",
      width: "150px"
    },
    {
      label: "单位充电成本(元/kWh)",
      prop: "unitChargeCost",
      width: "180px"
    },
    {
      label: "单位放电成本(元/kWh)",
      prop: "unitDischargeCost",
      width: "180px"
    },
    {
      label: "充电效率",
      prop: "chargeRatio",
      width: "120px"
    },
    {
      label: "放电效率",
      prop: "dischargeRatio",
      width: "120px"
    },
    {
      label: "自放电效率",
      prop: "selfDischargeRatio",
      width: "130px"
    },
    {
      label: "自用电效率",
      prop: "selfUseRatio",
      width: "130px"
    },
    {
      label: "最小充电时长(h)",
      prop: "minChargeTime",
      width: "150px"
    },
    {
      label: "最小放电时长(h)",
      prop: "minDischargeTime",
      width: "150px"
    },
    {
      label: "资源状态",
      prop: "status",
      width: "120px"
    },
    {
      label: "储能上报状态",
      prop: "storStatus",
      width: "140px"
    },
    {
      label: "首次上线时间",
      prop: "initOnlineTime",
      width: "140px"
    },
    {
      label: "上线时间",
      prop: "onlineTime",
      width: "120px"
    },
    {
      label: "下线时间",
      prop: "offlineTime",
      width: "120px"
    },
    {
      label: "有功遥测点号",
      prop: "pKey",
      width: "140px"
    },
    {
      label: "无功遥测点号",
      prop: "qKey",
      width: "140px"
    },
    {
      label: "SOC遥测点号",
      prop: "socKey",
      width: "140px"
    },
    {
      label: "储能状态点号",
      prop: "storStatusKey",
      width: "140px"
    },
    {
      label: "总利润(元)",
      prop: "totalRevenue",
      width: "100px"
    },
    {
      label: "分成利润(元)",
      prop: "shareRevenue",
      width: "120px"
    },
    {
      label: "总有功(kW)",
      prop: "totalP",
      width: "120px"
    },
    {
      label: "总无功(kVar)",
      prop: "totalQ",
      width: "120px"
    },
    {
      label: "联系人",
      prop: "contactName",
      width: "120px"
    },
    {
      label: "联系电话",
      prop: "contactTel",
      width: "120px"
    }
  ]
});
const loading = ref(false);
const multipleTableRef = ref<InstanceType<typeof ElTable>>();
const multipleSelection = ref([]);
const checkAll = ref(true);
const isIndeterminate = ref(false);
const checkedCities = ref([]);

const handleCheckAllChange = (val: boolean) => {
  checkedCities.value = val ? defaultColums.data.map(item => item.prop) : [];
  isIndeterminate.value = false;
  if (checkedCities.value.length) {
    colums.data = defaultColums.data;
  } else {
    colums.data = [];
  }
};
const pageObj = reactive({
  data: {
    currentPage: 1,
    pageSize: 10,
    total: 0
  }
});
const handleCheckedCitiesChange = (value: string[]) => {
  const checkedCount = value.length;
  checkAll.value = checkedCount === defaultColums.data.length;
  isIndeterminate.value =
    checkedCount > 0 && checkedCount < defaultColums.data.length;
  let cols = [];
  defaultColums.data.forEach(i => {
    if (value.indexOf(i.prop) > -1) {
      cols.push(i);
    }
  });
  colums.data = cols;
};
const lookRow = row => {
  addDialog.value = true;
  status.value = "look";
  title.value = "查看储能";
  let data = cloneDeep(row);
  form.data = data;
};
onMounted(() => {
  if (props.activeVppUserId && props.activeVppUserId !== "null") {
    search();
  }
  checkedCities.value = colums.data.map(item => item.prop);
});

const form = reactive({
  data: {
    id: null,
    name: null,
    vppUserId: null,
    storSeqNo: null,
    storMaker: null,
    storEmsMaker: null,
    storEmsIpadd: null,
    longitude: null,
    latitude: null,
    longitudeAndLatitude: null,
    maxLoad: null,
    maxOutput: null,
    maxAdjustStep: null,
    normalAdjustSpeed: null,
    maxUpSpeed: null,
    maxDownSpeed: null,
    socUpperLimit: null,
    socLowerLimit: null,
    unitChargeCost: null,
    unitDischargeCost: null,
    chargeRatio: null,
    dischargeRatio: null,
    selfDischargeRatio: null,
    selfUseRatio: null,
    minChargeTime: null,
    minDischargeTime: null,
    status: null,
    storStatus: null,
    initOnlineTime: null,
    onlineTime: null,
    offlineTime: null,
    pKey: null,
    qKey: null,
    socKey: null,
    storStatusKey: null,
    totalRevenue: null,
    shareRevenue: null,
    totalP: null,
    totalQ: null,
    contactName: null,
    contactTel: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  },
  rules: {
    name: [{ required: true, message: "储能名称不能为空", trigger: "blur" }],
    storMaker: [
      { required: true, message: "储能厂家不能为空", trigger: "blur" }
    ],
    storEmsMaker: [
      { required: true, message: "储能EMS厂家不能为空", trigger: "blur" }
    ],
    initOnlineTime: [
      { required: true, message: "首次上线时间不能为空", trigger: "change" }
    ],
    onlineTime: [
      { required: true, message: "上线时间不能为空", trigger: "change" }
    ],
    offlineTime: [
      { required: true, message: "下线时间不能为空", trigger: "change" }
    ]
  }
});
const ruleForm = ref<InstanceType<typeof ElForm>>();
const addDialog = ref(false);
const status = ref("add");
const title = ref("新增储能");
const addMenu = () => {
  addDialog.value = true;
  status.value = "add";
  title.value = "新增储能";
};
const handleSelectionChange = val => {
  multipleSelection.value = val;
};
const editRow = async row => {
  addDialog.value = true;
  status.value = "edit";
  title.value = "修改储能";
  let data = cloneDeep(row);
  form.data = data;
};

const delSelect = () => {
  if (multipleSelection.value.length) {
    ElMessageBox.confirm(
      `确认删除选中的${multipleSelection.value.length}条数据?`,
      "系统提示",
      {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        dangerouslyUseHTMLString: true,
        draggable: true
      }
    ).then(async () => {
      await delVppStorageList(multipleSelection.value.map(i => i.id));
      ElNotification({
        title: "系统提示",
        message: "删除成功",
        type: "success"
      });
      search();
    });
  }
};
const editSelect = () => {
  if (multipleSelection.value.length === 1) {
    editRow(multipleSelection.value[0]);
  }
};
const delConfirm = async row => {
  await delVppStorageList([row.id]);
  ElNotification({
    title: "系统提示",
    message: "删除成功",
    type: "success"
  });
  search();
};
const cancel = () => {
  form.data = {
    id: null,
    name: null,
    vppUserId: null,
    storSeqNo: null,
    storMaker: null,
    storEmsMaker: null,
    storEmsIpadd: null,
    longitude: null,
    latitude: null,
    longitudeAndLatitude: null,
    maxLoad: null,
    maxOutput: null,
    maxAdjustStep: null,
    normalAdjustSpeed: null,
    maxUpSpeed: null,
    maxDownSpeed: null,
    socUpperLimit: null,
    socLowerLimit: null,
    unitChargeCost: null,
    unitDischargeCost: null,
    chargeRatio: null,
    dischargeRatio: null,
    selfDischargeRatio: null,
    selfUseRatio: null,
    minChargeTime: null,
    minDischargeTime: null,
    status: null,
    storStatus: null,
    initOnlineTime: null,
    onlineTime: null,
    offlineTime: null,
    pKey: null,
    qKey: null,
    socKey: null,
    storStatusKey: null,
    totalRevenue: null,
    shareRevenue: null,
    totalP: null,
    totalQ: null,
    contactName: null,
    contactTel: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  ruleForm.value.resetFields();
  nextTick(() => {
    addDialog.value = false;
  });
};
const beforeClose = d => {
  form.data = {
    id: null,
    name: null,
    vppUserId: null,
    storSeqNo: null,
    storMaker: null,
    storEmsMaker: null,
    storEmsIpadd: null,
    longitude: null,
    latitude: null,
    longitudeAndLatitude: null,
    maxLoad: null,
    maxOutput: null,
    maxAdjustStep: null,
    normalAdjustSpeed: null,
    maxUpSpeed: null,
    maxDownSpeed: null,
    socUpperLimit: null,
    socLowerLimit: null,
    unitChargeCost: null,
    unitDischargeCost: null,
    chargeRatio: null,
    dischargeRatio: null,
    selfDischargeRatio: null,
    selfUseRatio: null,
    minChargeTime: null,
    minDischargeTime: null,
    status: null,
    storStatus: null,
    initOnlineTime: null,
    onlineTime: null,
    offlineTime: null,
    pKey: null,
    qKey: null,
    socKey: null,
    storStatusKey: null,
    totalRevenue: null,
    shareRevenue: null,
    totalP: null,
    totalQ: null,
    contactName: null,
    contactTel: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  nextTick(() => {
    d();
  });
};
const resetForm = () => {
  form.data = {
    id: null,
    name: null,
    vppUserId: null,
    storSeqNo: null,
    storMaker: null,
    storEmsMaker: null,
    storEmsIpadd: null,
    longitude: null,
    latitude: null,
    longitudeAndLatitude: null,
    maxLoad: null,
    maxOutput: null,
    maxAdjustStep: null,
    normalAdjustSpeed: null,
    maxUpSpeed: null,
    maxDownSpeed: null,
    socUpperLimit: null,
    socLowerLimit: null,
    unitChargeCost: null,
    unitDischargeCost: null,
    chargeRatio: null,
    dischargeRatio: null,
    selfDischargeRatio: null,
    selfUseRatio: null,
    minChargeTime: null,
    minDischargeTime: null,
    status: null,
    storStatus: null,
    initOnlineTime: null,
    onlineTime: null,
    offlineTime: null,
    pKey: null,
    qKey: null,
    socKey: null,
    storStatusKey: null,
    totalRevenue: null,
    shareRevenue: null,
    totalP: null,
    totalQ: null,
    contactName: null,
    contactTel: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
};
const submit = () => {
  ruleForm.value.validate(async valid => {
    if (valid) {
      let data = cloneDeep(form.data);
      data.vppUserId = props.activeVppUserId;
      if (status.value === "add") {
        await addVppStorageList(data);
        ElNotification({
          title: "系统提示",
          message: "终端创建成功",
          type: "success"
        });
      } else {
        await editVppStorageList(data);
        ElNotification({
          title: "系统提示",
          message: "终端编辑成功",
          type: "success"
        });
      }
      resetForm();
      nextTick(() => {
        addDialog.value = false;
        search();
      });
    }
  });
};
</script>

<style scoped lang="scss">
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}

:deep(.el-button:focus-visible) {
  outline: none;
}

.main-content {
  margin: 24px 24px 0 !important;
}

.search-form {
  :deep(.el-form-item) {
    margin-right: 4px;
    margin-bottom: 0;
  }
}
</style>
